package org.hengguan.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

public class DefaultCodeGenerator {

	private static Scanner scanner = new Scanner(System.in);
	
	private static String scanner(String title) {
		
		StringBuilder tips = new StringBuilder();
		tips.append("请输入").append(title);
		System.out.println(tips.toString());
		String input = scanner.nextLine();
		if (StringUtils.isNotBlank(input)) {
			return input;
		}
		scanner.close();
		throw new MybatisPlusException("请输入正确的"+title+"!!!");
	}
	
	
	public static void main(String[] args) {
		//代码生成器
		AutoGenerator autoGenerator = new AutoGenerator();
		//全局配置
		GlobalConfig globalConfig = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		
		globalConfig.setOutputDir(projectPath+"/src/main/java");
		
		globalConfig.setOpen(false);
		globalConfig.setSwagger2(true);
		globalConfig.setActiveRecord(true);
		globalConfig.setDateType(DateType.ONLY_DATE);
		globalConfig.setFileOverride(true);
		globalConfig.setEntityName("%sModel");
		globalConfig.setServiceImplName("%sService");
		//数据源配置
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL);
		dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/hengguan?serverTimezone=Asia/Shanghai");
		dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
		dataSourceConfig.setUsername("root");
		dataSourceConfig.setPassword("123456");
		//包管理配置
		PackageConfig packageConfig = new PackageConfig();
		
		//拆包模式
		String scan = scanner("模块名...");
		packageConfig.setEntity("model."+scan);
		packageConfig.setMapper("mapper."+scan);
		packageConfig.setXml("mapper."+scan+".xml");
		packageConfig.setService("service."+scan+".i");
		packageConfig.setServiceImpl("service."+scan);
		packageConfig.setController("controller."+scan);
//		模块模式
//		packageConfig.setModuleName("demo");
		//模板配置
//		TemplateConfig templateConfig = new TemplateConfig();
		//策略配置
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setNaming(NamingStrategy.underline_to_camel);
		strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
		strategyConfig.setEntityLombokModel(true);
		strategyConfig.setRestControllerStyle(true);
		strategyConfig.setChainModel(true);
		strategyConfig.setEntityTableFieldAnnotationEnable(true);
		//表名
		strategyConfig.setInclude(scanner("表名,多个英文逗号分隔...").split(","));
		
		//上边配置基本上不用调整了
		//**********************************
		
		globalConfig.setIdType(IdType.AUTO);
		strategyConfig.setTablePrefix("tb_contest_");
		packageConfig.setParent("org.hengguan.base.contest");
		globalConfig.setAuthor("hsp");
		//**********************************
		
		autoGenerator.setDataSource(dataSourceConfig);
		autoGenerator.setGlobalConfig(globalConfig);
		autoGenerator.setPackageInfo(packageConfig);
//		autoGenerator.setTemplate(templateConfig);
		autoGenerator.setStrategy(strategyConfig);
		scanner.close();
		autoGenerator.execute();
	}
}
